package cn.shengchao.examstar.knowledge.domain.service;

import cn.shengchao.examstar.knowledge.domain.entity.KnowledgePointMastery;
import cn.shengchao.examstar.knowledge.domain.repository.KnowledgePointMasteryRepository;
import cn.shengchao.examstar.knowledge.domain.vo.MasteryStatusEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * 知识点掌握度领域服务
 */
@Service
@RequiredArgsConstructor
public class KnowledgePointMasteryService {

    private final KnowledgePointMasteryRepository masteryRepository;

    /**
     * 保存知识点掌握度
     */
    @Transactional
    public KnowledgePointMastery saveMastery(KnowledgePointMastery mastery) {
        return masteryRepository.save(mastery);
    }

    /**
     * 更新知识点掌握度
     * 如果不存在则创建新的记录
     */
    @Transactional
    public KnowledgePointMastery updateMastery(Long userId, Long knowledgePointId,
                                               String newDegreeOfMastery, String accuracyRate,
                                               Long studyPlanId) {
        // 查询现有掌握度信息
        Optional<KnowledgePointMastery> existingMastery = masteryRepository.findByUserIdAndKnowledgePointId(userId, knowledgePointId);

        KnowledgePointMastery mastery;
        if (existingMastery.isPresent()) {
            // 更新现有掌握度
            mastery = existingMastery.get();
            mastery.setDegreeOfMastery(newDegreeOfMastery);
            mastery.setAccuracyRate(accuracyRate);
            // 如果传入了学习计划ID，则更新
            if (studyPlanId != null) {
                mastery.setStudyPlanId(studyPlanId);
            }
        } else {
            // 创建新的掌握度记录
            mastery = KnowledgePointMastery.builder()
                    .userId(userId)
                    .knowledgePointId(knowledgePointId)
                    .degreeOfMastery(newDegreeOfMastery)
                    .accuracyRate(accuracyRate)
                    .studyPlanId(studyPlanId)
                    .status(MasteryStatusEnum.NEW.getCode()) // 使用枚举：默认状态为新建
                    .build();
        }

        return masteryRepository.save(mastery);
    }

    /**
     * 更新知识点掌握度状态
     *
     * @param userId 用户ID
     * @param knowledgePointId 知识点ID
     * @param status 状态枚举
     * @return 更新后的知识点掌握度
     */
    @Transactional
    public KnowledgePointMastery updateMasteryStatus(Long userId, Long knowledgePointId, MasteryStatusEnum status) {
        // 查询现有掌握度信息
        Optional<KnowledgePointMastery> existingMastery = masteryRepository.findByUserIdAndKnowledgePointId(userId, knowledgePointId);

        KnowledgePointMastery mastery;
        if (existingMastery.isPresent()) {
            // 更新现有掌握度状态
            mastery = existingMastery.get();
            mastery.setStatusEnum(status);
        } else {
            // 创建新的掌握度记录
            mastery = KnowledgePointMastery.builder()
                    .userId(userId)
                    .knowledgePointId(knowledgePointId)
                    .status(status.getCode())
                    .build();
        }

        return masteryRepository.save(mastery);
    }

    /**
     * 批量更新知识点掌握度
     */
    @Transactional
    public void batchUpdateMastery(List<KnowledgePointMastery> masteryList) {
        masteryRepository.batchUpdateMastery(masteryList);
    }

    /**
     * 批量更新知识点掌握度状态
     */
    @Transactional
    public void batchUpdateMasteryStatus(Long userId, List<Long> knowledgePointIds, MasteryStatusEnum status) {
        for (Long knowledgePointId : knowledgePointIds) {
            updateMasteryStatus(userId, knowledgePointId, status);
        }
    }

    /**
     * 根据用户ID和知识点ID获取掌握度信息
     */
    public Optional<KnowledgePointMastery> getMasteryByUserAndKnowledgePoint(Long userId, Long knowledgePointId) {
        return masteryRepository.findByUserIdAndKnowledgePointId(userId, knowledgePointId);
    }

    /**
     * 根据用户ID获取所有掌握度信息
     */
    public List<KnowledgePointMastery> getMasteriesByUser(Long userId) {
        return masteryRepository.findByUserId(userId);
    }

    /**
     * 根据学习计划ID获取掌握度信息
     */
    public List<KnowledgePointMastery> getMasteriesByStudyPlan(Long studyPlanId) {
        return masteryRepository.findByStudyPlanId(studyPlanId);
    }

    /**
     * 删除掌握度信息
     */
    @Transactional
    public void deleteMastery(Long id) {
        masteryRepository.delete(id);
    }
} 